<?php
namespace common\components\services\goods;
use common\components\helpers\Fun;
use frontend\models\datas\ClassManage;
use frontend\models\datas\Goods;
use yii\base\Action;


class DeleteAction extends Action{
    public $type;
    public function run(){
        //逻辑删除，将is_delete字段更改即可
        if(\Yii::$app->request->get('id') == null){
            throw new \yii\web\BadRequestHttpException(\Yii::t('frontend','商品id为空'));
        }
        if(\Yii::$app->request->get('type_id') == null){
            throw new \yii\web\BadRequestHttpException(\Yii::t('frontend','商品类型不存在'));
        }
        $classmange = ClassManage::find()->where(['is_old'=>0,'associate_id'=>\Yii::$app->request->get('id'),'is_delete'=>0])->one();
        if($classmange !== null){
            throw new \yii\web\BadRequestHttpException(\Yii::t('frontend','存在班级不能删除！'));
        }
        $this->type = \Yii::$app->request->get('type_id');
        $goods = Goods::findOne(\Yii::$app->request->get('id'));
        $goods->is_delete = 1;
        if($goods->save(false)){
            $url = $this->controller->id;
            $url .= '/index';
            return $this->controller->redirectMessage('删除成功',Fun::url([$url,'type_id'=>$this->type]));
        }
    }
}
?>
